package com.group02.community.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group02.community.annotation.Log;
import com.group02.community.enmus.BusinessType;
import com.group02.community.entity.user.SysPost;
import com.group02.community.service.IPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api
@RestController
@CrossOrigin
@RequestMapping("/post")
public class PostController {

    @Autowired
    private IPostService service;

    @ApiOperation("分页")
    @GetMapping("/page")
    public ResponseEntity<IPage<SysPost>> getPage(Long current, Long pageSize) {
        System.out.println("执行了分页");
        IPage<SysPost> page = service.getPage(current, pageSize);
        return ResponseEntity.ok(page);
    }

    @ApiOperation("岗位添加")
    @PostMapping("/add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    public ResponseEntity<String> addPost(@RequestBody SysPost post) {
        System.out.println(post);
        System.out.println("执行了添加");
        service.save(post);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("岗位修改")
    @PutMapping("/update")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    public ResponseEntity<String> updatePost(@RequestBody SysPost post) {
        System.out.println("执行了修改");
        String postId = post.getPostId() + "";
        String status = post.getStatus();
        if (status.equals("1")) {
            Long num = service.getUserByPostId(postId);
            if (num != 0) {
                return ResponseEntity.ok("no");
            }
        }
        service.updateById(post);
        return ResponseEntity.ok("ok");
    }

    @GetMapping("posts")
    public ResponseEntity<List<SysPost>> getAllRole() {
        List<SysPost> list = service.list(null);
        return ResponseEntity.ok(list);
    }

    @ApiOperation("岗位删除")
    @DeleteMapping("delete/{postId}")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    public ResponseEntity<String> deletePost(@PathVariable String postId) {
        System.out.println("执行了删除");
        Long num = service.getUserByPostId(postId);
        if (num != 0) {
            return ResponseEntity.ok("no");
        }
        service.iaDelete(postId);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("模糊查询")
    @GetMapping("/query")
    public ResponseEntity<List<SysPost>> getPostPage(String postCode, String postName, String status) {
        List<SysPost> queryPage = service.getQueryPage(postCode, postName, status);
        return ResponseEntity.ok(queryPage);
    }

    @ApiOperation("多选删除")
    @DeleteMapping("/deleteAll/{ids}")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    public ResponseEntity<String> deleteGroup(@PathVariable List<String> ids) {
        System.out.println("执行了删除");
        Long num = null;
        for (int i = 0; i < ids.size(); i++) {
            String s = ids.get(i);
            System.out.println(s);
            num = service.getUserByPostId(s);
            if (num != 0) {
                return ResponseEntity.ok(s);
            }
        }
        System.out.println("结果为：" + num);
        service.deleteGroup(ids);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("导出excel")
    @GetMapping("/excel")
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    public ResponseEntity<String> exportExcel(HttpServletResponse response) throws IOException {
        service.exportExcel(response);
        System.out.println("进到文件下载了");
        return ResponseEntity.ok("ok");
    }
}
